<?php

namespace libraries;

use enums\ServiceEnums;
/**
 * 
 * @author fzq
 * @comment for swoole
 * @2016-10-08
 */
class SwooleHeader extends \Phalcon\Di\InjectionAwareInterface
{	
	private $_di;
	
	const SWOOLE_HTTP_SERVER = 'HttpServer';
	
	const EXPIRE_TIME_FORMAT = 'D, d M Y H:i:s GMT';
	
	public function __construct( $di = null )
	{
		if( isset( $di ) )
		{
			$this->_di = $di;
		}
		else
		{
			throw new \Exception( '必须指定di' );
				
			return;
		}
		
	}
	
	/**
	 * Redirect by HTTP to another action or URL
	 * <code>
	 * //Using a string redirect (internal/external)
	 * $response->redirect("posts/index");
	 * $response->redirect("http://en.wikipedia.org", true);
	 * $response->redirect("http://www.example.com/new-location", true, 301);
	 * //Making a redirection based on a named route
	 * $response->redirect(array(
	 * "for" => "index-lang",
	 * "lang" => "jp",
	 * "controller" => "index"
	 * ));
	 * </code>
	 *
	 * @param mixed $location
	 * @param bool $externalRedirect
	 * @param int $iStatusCode
	 * @return Response
	 */
	public function redirect( $strUrl, $bExternalRedirect = false, $iStatusCode = 302 )
	{
		if( class_exists( self::SWOOLE_HTTP_SERVER ) )
		{
			if( is_string( $strUrl ))
			{
				\HttpServer::$response->header( 'Location', $strUrl );
			}
			
			if( is_int( $iStatusCode ))
			{
				\HttpServer::$response->status( $iStatusCode );
			}
		}
		else
		{
			return $this->_di['response']->redirect( $strUrl, $bExternalRedirect, $iStatusCode );
		}
		
	}
	
	public function setStatusCode( $iStatusCode )
	{
		if( class_exists( self::SWOOLE_HTTP_SERVER ))
		{
			if( is_int( $iStatusCode ) )
			{
				\HttpServer::$response->status( $iStatusCode );
			}
		}
		else
		{
			return $this->_di['response']->setStatusCode( $iStatusCode );
		}
	}
	
	public function setHeader( $strKey, $val )
	{
		if( class_exists( self::SWOOLE_HTTP_SERVER ))
		{
			if( is_string( $strKey ) )
			{
				\HttpServer::$response->header( $strKey, $val );
			}
		}
		else
		{
			return $this->_di['response']->setHeader( $strKey, $val );
		}
	}
	
	/**
	 * 重新调用setHeaders会覆盖上一次的设置
	 * @param array $arrKV
	 */
	public static function setHeaders( array $arrKV )
	{
		if( is_array( $arrKV ))
		{
			\HttpServer::$response->setHeaders( $arrKV );
		}
	}
	
	public function setCookie( $key,
			$value = '', 
			$expire = 0, 
			$path = '/', 
			$domain  = '', 
			$secure = false , 
			$httponly = false )
	{
		$this->_di[ ServiceEnums::SERVICE_COOKIES ]->set( $key, $value, $expire, $path, $secure, $domain, $httponly );
	}
	
	/**
	 * Sets a Expires header to use HTTP cache
	 * <code>
	 * $this->response->setExpires(new DateTime());
	 * </code>
	 *
	 * @param mixed $datetime
	 * @return Response
	 */
	public function setExpires( $dateTime = null )
	{
		if( isset( $dateTime ) )
		{		
	// 		$dateTime = new \DateTime();
			
			if( class_exists( self::SWOOLE_HTTP_SERVER ))
			{
	// 				$t = $_SERVER[ 'HTTP_REQUEST_TIME' ] + $ttl;
	// 				$dateTime::format( self::EXPIRE_TIME_FORMAT );
	// 				$strDateTime = gmdate( self::EXPIRE_TIME_FORMAT, $t );
					
			// 		header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
			// 		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
				self::setHeader( 'Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate' );
				self::setHeader( 'Expires', $dateTime->format( self::EXPIRE_TIME_FORMAT ) );
			}
			else
			{
				return $this->_di['response']->setExpires( $dateTime );
			}
			
		}
	}
	
	/**
	 * Send a raw header to the response
	 * <code>
	 * $response->setRawHeader("HTTP/1.1 404 Not Found");
	 * </code>
	 *
	 * @param string $header
	 * @return Response
	 */
// 	public function setRawHeader($header) {}
	
	/**
	 * Resets all the stablished headers
	 *
	 * @return Response
	 */
// 	public function resetHeaders() {}
	

	/**
	 * Sets Last-Modified header
	 * <code>
	 * $this->response->setLastModified(new DateTime());
	 * </code>
	 *
	 * @param mixed $datetime
	 * @return Response
	 */
	public function setLastModified( $dateTime = null ) 
	{
		if( isset( $dateTime ) )
		{
			if( class_exists( self::SWOOLE_HTTP_SERVER ))
			{
				//最后修改时间(在缓存的时候可以用到)
				// last modified (good for caching)
	// 			$time = $_SERVER[ 'HTTP_REQUEST_TIME' ] + $ttl; // or filemtime($fn), etc
				//header('Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $time ).' GMT');
				\HttpServer::$response->header( 'Last-Modified',$dateTime->format( 'D, d M Y H:i:s' ) );
			}
			else
			{
				return $this->_di['response']->setLastModified( $dateTime );
			}
		}	
	}
	
	/**
	 * Sends a Not-Modified response
	 *
	 * @return Response
	 */
	public function setNotModified() 
	{
		if( class_exists( self::SWOOLE_HTTP_SERVER ) )
		{
			self::setStatusCode( 304 );
		}
		else
		{
			return $this->_di['response']->setStatusCode( 304 );
		}
	}
	
	/**
	 * Sets the response content-type mime, optionally the charset
	 * <code>
	 * $response->setContentType('application/pdf');
	 * $response->setContentType('text/plain', 'UTF-8');
	 * </code>
	 *
	 * @param string $contentType
	 * @param mixed $charset
	 * @return Response
	 */
	public function setContentType( $strContentType = null ) 
	{
		if( isset( $strContentType ) )
		{
			if( class_exists( self::SWOOLE_HTTP_SERVER ))
			{
				self::setHeader( 'Content-Type', $strContentType );
			}
			else
			{
				return $this->_di['response']->setContentType( $strContentType );
			}
		}
	}
	
	/**
	 * Sets the response content-length
	 * <code>
	 * $response->setContentLength(2048);
	 * </code>
	 *
	 * @param int $contentLength
	 * @return Response
	 */
	public function setContentLength( $iContentLength = 0 ) 
	{
		if( class_exists( self::SWOOLE_HTTP_SERVER ))
		{
			self::setHeader( 'Content-Length', $iContentLength );
		}
		else
		{
			return $this->_di['response']->setContentLength( $iContentLength );
		}
	}
	
	/**
	 * Set a custom ETag
	 * <code>
	 * $response->setEtag(md5(time()));
	 * </code>
	 *
	 * @param string $etag
	 * @return Response
	 */
	public function setEtag( $strETag ) 
	{
		if( class_exists( self::SWOOLE_HTTP_SERVER ) )
		{
			self::setHeader( 'Etag', $strETag );
		}
		else
		{
			return $this->_di['response']->setEtag( $strETag );
		}
	}
	
	/* (non-PHPdoc)
	 * @see \Phalcon\Di\InjectionAwareInterface::setDI()
	 */
	public function setDI(\Phalcon\DiInterface $dependencyInjector) 
	{
		$this->_di = $dependencyInjector;
	}

	/* (non-PHPdoc)
	 * @see \Phalcon\Di\InjectionAwareInterface::getDI()
	 */
	public function getDI() 
	{
		return $this->_di;
	}

	
}